﻿using System;
using System.Collections.Generic;
using System.Linq;
using DotNetNuke.Common.Utilities;
using DotNetNuke.ComponentModel;
using DotNetNuke.Data;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework.Providers;
using DotNetNuke.Services.Cache;

namespace DotNetNuke.Modules.DNNFiddle.Data
{

    public class FiddleDataService : ComponentBase<IFiddleDataService, FiddleDataService>, IFiddleDataService
    {

        #region ComponentBase Implementation

        public static void LoadProviderSettings()
        {
            ComponentFactory.InstallComponents(new ProviderInstaller("data", typeof(DataProvider)));
        }

        private readonly DataProvider _provider;

        public FiddleDataService()
        {
            _provider = DataProvider.Instance();
        }

        #endregion

        #region Private methods

        private string ProcRef(string name)
        {
            return string.Format("{0}{1}", _provider.ObjectQualifier, name);
        }

        #endregion

        #region IFiddleDataService Implementation

        public FiddleInfo GetFiddle(int moduleId)
        {
            return CBO.FillObject<FiddleInfo>(_provider.ExecuteReader(ProcRef("Fiddle_Get"), moduleId));
        }

        public FiddleViewModel GetFiddleDTO(int moduleId)
        {
            FiddleViewModel fvm = null;
            FiddleInfo fiddle = GetFiddle(moduleId);
            List<FiddleScriptsInfo> fiddlescripts = ListFiddleScripts(moduleId);
            if (fiddle != null)
            {
                fvm = new FiddleViewModel()
                {
                    moduleId = fiddle.ModuleId,
                    html = fiddle.Html,
                    css = fiddle.Css,
                    script = fiddle.Javascript
                };
                foreach (FiddleScriptsInfo fs in fiddlescripts)
                {
                    ScriptViewModel svm = new ScriptViewModel()
                    {
                        scriptid = fs.ScriptId,
                        name = fs.Name,
                        url = fs.Url,
                        order = fs.Order,
                        enabled = fs.Enabled,
                        canDelete = fs.CanDelete
                    };

                    fvm.scripts.Add(svm);
                }
            }
            return fvm;
        }

        public List<FiddleScriptsInfo> ListFiddleScripts(int moduleId)
        {
            return CBO.FillCollection<FiddleScriptsInfo>(_provider.ExecuteReader(ProcRef("FiddleScripts_List"), moduleId));
        }

        public void DeleteFiddle(int moduleId)
        {
            _provider.ExecuteNonQuery(ProcRef("Fiddle_Delete"), moduleId);
        }

        public void DeleteFiddleScript(int scriptId)
        {
            _provider.ExecuteNonQuery(ProcRef("FiddleScripts_Delete"), scriptId);
        }

        public void SaveFiddle(FiddleInfo fiddleInfo)
        {

            UserInfo user = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();
            _provider.ExecuteNonQuery(ProcRef("Fiddle_Update"),
                fiddleInfo.ModuleId,
                fiddleInfo.Html,
                fiddleInfo.Css,
                fiddleInfo.Javascript,
                user.UserID,
                DateTime.Now);
        }

        public int SaveFiddleScript(int moduleId, int userID, FiddleScriptsInfo script)
        {
            return _provider.ExecuteScalar<int>(ProcRef("FiddleScripts_Update"),
                                Null.GetNull(script.ScriptId, DBNull.Value),
                                moduleId,
                                script.Name,
                                script.Url,
                                script.Order,
                                script.Enabled,
                                script.CanDelete,
                                userID,
                                DateTime.Now);
        }

        #endregion
    }
}